package component;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import diagram.NodeElement;
import window.CollieModelPanel;

public class ModelAtributPanel extends JPanel{

	private final DefaultTableModel attributesTableModel;

	private final JTable attributesTable;

	private Vector model;
	
	public List getAtributList(){
	
		attributesTableModel.getDataVector();
	
		List attributeList = new ArrayList();
		
		for(int i=0; i < attributesTableModel.getDataVector().size(); i++ ){

			// on récupère la valeur contenu dans notre fenêtre pop up 
			Vector vector = (Vector) attributesTableModel.getDataVector().get(i);
			
			String valeur0 = (String) vector.get(0);
			
			// on enlève toutes les lignes ou la valeur est nulle ou égale à un espace
			if(valeur0 !=null && valeur0.trim() != "")
				attributeList.add(vector.toArray(new String[2]));
		}
		
		// on retourne la liste des atribus 
		return attributeList; 
	
	}
	
	/*
	 * Constructeur 
	 */
	public ModelAtributPanel(List atributeList) {
		model = new Vector();
		for(int i=0;i<atributeList.size();i++){
			String[] paire = (String[])atributeList.get(i);
			Vector vector = new Vector();
			vector.add(paire[0]);
			vector.add(paire[1]);
			model.add(vector);
		}
		// Configuration of the attributes table
		// Table Header
		final Vector<String> headers = new Vector<String>();
		headers.add("Attribute name");
		headers.add("Attribute value");
		// Instantiate the table model
		attributesTableModel = new DefaultTableModel(model,headers);

		// Instantiate the Table component
		attributesTable = new JTable(attributesTableModel);
		// Let the table fill the vertical space
		attributesTable.setFillsViewportHeight(true);
		// Listen to double-click to add a new row to the table
		attributesTable.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(final MouseEvent pE) {
				if (1 < pE.getClickCount()) {
					// double click
					final int numberOfRows = attributesTableModel.getRowCount();
					if (numberOfRows == 0) {
						attributesTableModel.insertRow(numberOfRows,
								new String[2]);
					} else {
						final Object lastAttribute = attributesTableModel
								.getValueAt(numberOfRows - 1, 0);
						// if the attribute name of the last row is set
						if ((null != lastAttribute)
								&& !"".equals(lastAttribute.toString().trim())) {
							// then add a new row to the model
							attributesTableModel.insertRow(numberOfRows,
									new String[2]);
						}
					}
				}
			}
		});
		// Use a scrolling pane to render properly the table and its header
		final JScrollPane scrollPane = new JScrollPane(attributesTable);
		// Add the scrolling pane to this property panel
		add(scrollPane, BorderLayout.CENTER);
	}

}
